package com.dinadi.yidi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.CommentRequest;
import com.dinadi.yidi.ui.activity.DesignerHmePageActivity;
import com.dinadi.yidi.ui.activity.DesignerInformationActivity;
import com.dinadi.yidi.ui.activity.ReplyListActivity;
import com.dinadi.yidi.utils.FormatCurrentData;
import com.dinadi.yidi.utils.ImageUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/4/7
 * Time: 17:33
 */
public class DailyProductAdapter extends BaseAdapter {

    private Context context;
    private List<CommentRequest> mList = new ArrayList<>();
    private String result;

    public DailyProductAdapter(Context context, List<CommentRequest> mList) {
        this.context = context;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_daily_product_listview_item, null); /**这个是评论的item列表*/
            viewHolder = new ViewHolder();
            viewHolder.designer_nick_name = (TextView) convertView.findViewById(R.id.designer_nick_name);
            viewHolder.creat_time = (TextView) convertView.findViewById(R.id.creat_time);
            viewHolder.head_img = (ImageView) convertView.findViewById(R.id.head_img);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.repeat_total_count = (TextView) convertView.findViewById(R.id.repeat_total_count);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.designer_nick_name.setText(mList.get(position).getUserName());
        String creatTime = mList.get(position).getCreateTime();
        final String showCreatTime = FormatCurrentData.getTimeRange(creatTime);
        viewHolder.creat_time.setText(showCreatTime);
        String str = mList.get(position).getContent();
        try {
            str = URLDecoder.decode(str, "utf-8");
//            str = URLDecoder.decode(str,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        viewHolder.content.setText(str);
        final String imgUrl = mList.get(position).getHeadImg();
        viewHolder.repeat_total_count.setText("查看" + mList.get(position).getRepeatTotalCount() + "条回复");
        if (imgUrl == null) {
            ImageUtils.loadImageView(context, R.drawable.me_img_default, viewHolder.head_img);

        } else {
            if (!imgUrl.equals("")) {
                ImageUtils.loadImageView(context, imgUrl, viewHolder.head_img);
            } else {
                ImageUtils.loadImageView(context, R.drawable.me_img_default, viewHolder.head_img);
            }
        }

        viewHolder.repeat_total_count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String commentId = mList.get(position).getCommentId();
                Intent intent = new Intent(context, ReplyListActivity.class);
                intent.putExtra("commentId", commentId);
                intent.putExtra("imgUrl", imgUrl);
                intent.putExtra("content", mList.get(position).getContent());
                intent.putExtra("nickName", mList.get(position).getUserName());
                intent.putExtra("creatTime", showCreatTime);
                intent.putExtra("toUserId", mList.get(position).getUserId());
                context.startActivity(intent);
            }
        });
        viewHolder.head_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mList.get(position).getUserType().equals("1")) {
                    Intent intent = new Intent(context, DesignerInformationActivity.class);
                    String dUserId = mList.get(position).getUserId();
                    intent.putExtra("dUserId", dUserId);
                    intent.putExtra("type", mList.get(position).getUserType());
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, DesignerHmePageActivity.class);
                    String dUserId = mList.get(position).getUserId();
                    intent.putExtra("dUserId", dUserId);
                    intent.putExtra("type", mList.get(position).getUserType());
                    context.startActivity(intent);
                }

            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView designer_nick_name, creat_time, content, repeat_total_count;
        ImageView head_img;


    }
}
